package com.koocloud.electroplatemanage.auth.mapper;


import com.koocloud.electroplatemanage.pojo.Operation;
import com.koocloud.electroplatemanage.pojo.Permission;
import com.koocloud.electroplatemanage.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface ILoginDao {
    // Insert
    @Insert("insert into user(username, password) values(#{username}, #{password})")
    public void insertUser(User user);

    // select
    @Results
    @Select("select * from user")
    public List<User> getUsers();

    @Select("select id,username,job_num,fullname,password,reg_phone,enable_fag,superior_id,department_id from user where username=#{username}")
    public User getUserById(@Param("username") String username);

    // update
    @Update("update user set password=#{password} where username=#{username}")
    public void updateUser(User authorityUser);

    // delete
    @Delete("delete from user where username=#{username}")
    public void deleteUser(@Param("username") String username);

    @Select("SELECT \r\n" + 
            "DISTINCT\r\n" + 
            "    m.id,\r\n" + 
            "    m.permission_name,\r\n" + 
            "    m.url,\r\n" + 
            "    m.icon,\r\n" + 
            "    m.ORDER,\r\n" + 
            "    m.parent_id,\r\n" + 
            "    m.lvl,\r\n" + 
            "    m.type \r\n" + 
            "FROM\r\n" + 
            "    USER u\r\n" + 
            "    INNER JOIN user_role ru ON u.id = ru.user_id\r\n" + 
            "    INNER JOIN role r ON ru.role_id = r.id\r\n" + 
            "    INNER JOIN role_permission rm ON r.id = rm.role_id\r\n" + 
            "    INNER JOIN permission m ON rm.permission_id = m.id\r\n" + 
            "WHERE\r\n" + 
            "    u.username =#{username}\r\n" + 
            "    and m.type != 2")
	public List<Permission> findTree(String username);

    
	@Select("select p.url from user u,user_role ur,role_permission rp,permission p where u.id=ur.user_id and ur.role_id=rp.role_id and rp.permission_id=p.id and u.username=#{userName}")
	public List<Map<String, Object>> selectAuthList(String username);

	
    @Select("select m.id,m.permission_name,m.url,m.icon,m.order,m.parent_id,m.lvl,m.type  from permission m")
	public List<Permission> findTree2();

    @Select("SELECT * FROM operation where menu_id=#{id}")
    public List<Operation> selectByMenuId(long id);

    @Select("SELECT * FROM permission where parent_id=#{id}")
    public List<Permission> selectChildrenById(long id);
    

    @Select("SELECT \r\n" + 
            "DISTINCT\r\n" + 
            "    m.id,\r\n" + 
            "    '查看' as permission_name,\r\n" + 
            "    m.url,\r\n" + 
            "    m.icon,\r\n" + 
            "    -99999 as `order`,\r\n" + 
            "    m.id as parent_id,\r\n" + 
            "    3 as lvl,\r\n" + 
            "    m.type \r\n" + 
            "FROM\r\n" + 
            "    USER u\r\n" + 
            "    INNER JOIN user_role ru ON u.id = ru.user_id\r\n" + 
            "    INNER JOIN role r ON ru.role_id = r.id\r\n" + 
            "    INNER JOIN role_permission rm ON r.id = rm.role_id\r\n" + 
            "    INNER JOIN permission m ON rm.permission_id = m.id\r\n" + 
            "WHERE\r\n" + 
            "    u.username = 'admin'\r\n" + 
            "    and m.type = 1\r\n" + 
            "UNION all\r\n" + 
            "SELECT \r\n" + 
            "DISTINCT\r\n" + 
            "    m.id,\r\n" + 
            "    m.permission_name,\r\n" + 
            "    m.url,\r\n" + 
            "    m.icon,\r\n" + 
            "    m.`ORDER`,\r\n" + 
            "    m.parent_id,\r\n" + 
            "    m.lvl,\r\n" + 
            "    m.type \r\n" + 
            "FROM\r\n" + 
            "    USER u\r\n" + 
            "    INNER JOIN user_role ru ON u.id = ru.user_id\r\n" + 
            "    INNER JOIN role r ON ru.role_id = r.id\r\n" + 
            "    INNER JOIN role_permission rm ON r.id = rm.role_id\r\n" + 
            "    INNER JOIN permission m ON rm.permission_id = m.id\r\n" + 
            "WHERE\r\n" + 
            "    u.username = 'admin'\r\n" + 
            "ORDER BY 1, 7")
    public List<Permission> findTreeBtn(String username);
}